package controller

import (
	"fmt"
	"gin_template/core/app"
	"gin_template/data/e"
	"gin_template/models"
	"gin_template/services"
	"net/http"

	"github.com/gin-gonic/gin"
)

// GetUserByCondition 根据查询条件获取用户列表信息
// @Summary 根据查询条件获取用户列表信息
// @Description 根据查询条件获取用户列表信息
// @Tags 用户列表接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 用户令牌"
// @Param object body models.User false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} app.Response
// @Router /getUserByCondition [post]
func GetUserByCondition(c *gin.Context) {
	appG := app.Gin{C: c}
	var queryParams models.GetUserRequest
	httpCode, errCode, validRes := app.BindAndValidWithError(c, &queryParams)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, validRes)
		return
	}
	result := services.QueryUserList(&queryParams)
	if result.Total != 0 {
		appG.Response(http.StatusOK, e.SUCCESS, result)
	} else {
		appG.Response(http.StatusNotFound, e.NOT_FOUNT, result)
	}

}

// InsertUser 新增用户
// @Summary 新增用户
// @Description 新增用户
// @Tags 用户新增接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 用户令牌"
// @Param object body models.User false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} app.Response
// @Router /insertUser [post]
func InsertUser(c *gin.Context) {
	appG := app.Gin{C: c}
	var insertUser models.User
	httpCode, errCode, validRes := app.BindAndValidWithError(c, &insertUser)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, validRes)
		return
	}
	id := services.InsertUser(&insertUser)
	if id != "" {
		appG.Response(http.StatusOK, e.SUCCESS, id)
	} else {
		appG.Response(http.StatusInternalServerError, e.ERROR)
	}
}

// DeleteUser 删除用户
// @Summary 删除用户
// @Description 删除用户
// @Tags 用户删除接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 用户令牌"
// @Param object body models.User false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} app.Response
// @Router /deleteUser [post]
func DeleteUser(c *gin.Context) {
	appG := app.Gin{C: c}
	var deleteModel models.UserPrimary
	httpCode, errCode, validRes := app.BindAndValidWithError(c, &deleteModel)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, validRes)
		return
	}
	if err := services.DeleteUser(&deleteModel); err != nil {
		fmt.Println(err)
		appG.Response(http.StatusInternalServerError, e.ERROR)
	} else {
		appG.Response(http.StatusOK, e.SUCCESS)
	}
}

// UpdateUser 更新用户信息
// @Summary 更新用户信息
// @Description 更新用户信息
// @Tags 用户更新接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 用户令牌"
// @Param object body models.User false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} app.Response
// @Router /updateUser [post]
func UpdateUser(c *gin.Context) {
	appG := app.Gin{C: c}
	var updateModel models.User
	httpCode, errCode, validRes := app.BindAndValidWithError(c, &updateModel)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, validRes)
		return
	}
	if updateInfo, err := services.UpdateUser(&updateModel); err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR)
	} else {
		appG.Response(http.StatusOK, e.SUCCESS, updateInfo)
	}
}

// GetUser 根据id查询用户信息
// @Summary 根据id查询用户信息
// @Description 根据id查询用户信息
// @Tags 用户信息查询接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 用户令牌"
// @Param object body models.User false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} app.Response
// @Router /getUser [post]
func GetUser(c *gin.Context) {
	appG := app.Gin{C: c}
	var queryUser models.UserPrimary
	httpCode, errCode, validRes := app.BindAndValidWithError(c, &queryUser)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, validRes)
		return
	}
	if userInfo, err := services.GetUser(&queryUser); err != nil {
		fmt.Println(err)
		appG.Response(http.StatusInternalServerError, e.ERROR)
	} else {
		fmt.Println("GetUser", userInfo)
		if userInfo == nil {
			appG.Response(http.StatusNotFound, e.NOT_FOUNT)
		} else {
			appG.Response(http.StatusOK, e.SUCCESS, userInfo)
		}
	}
}
